package com.swe.chumsmobile.activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.swe.chumsmobile.R;

public class TopCoursesActivity extends Activity {

	private static String SOAP_ACTION1 = "http://swe.cmpe.boun.edu.tr:8280/Kurum-Services-war-1.0/services/kurumservice";

	private static String NAMESPACE = "http://com.netas.kekmanager.service/";

	private static String METHOD_NAME1 = "KurumSorgulaRequest";

	private static String URL = "http://swe.cmpe.boun.edu.tr:8280/Kurum-Services-war-1.0/services/kurumservice?wsdl";

	private String name,user_name,email,register_date;
	private int id,user_type_id;
	private float average_rating;
	
	Button btnFar, btnCel, btnClear;
	TextView txtFar, txtCel;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();

		StrictMode.setThreadPolicy(policy);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.top_courses);

		btnCel = (Button) findViewById(R.id.btnCel);
		txtCel = (TextView) findViewById(R.id.topCoursesID);

		btnCel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// Initialize soap request + add parameters
				SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

				// Use this to add parameters
				request.addProperty("username", "user");
				request.addProperty("password", "user");

				// Declare the version of the SOAP request
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);

				envelope.setOutputSoapObject(request);
				envelope.dotNet = false;

				try {
					HttpTransportSE androidHttpTransport = new HttpTransportSE(
							URL);

					// this is the actual part that will call the webservice
					androidHttpTransport.call(SOAP_ACTION1, envelope);

					// Get the SoapResult from the envelope body.
					SoapObject result = (SoapObject) envelope.bodyIn;

					if (result != null) {
						SoapObject temp = (SoapObject) result.getProperty(0);
						id=Integer.parseInt(temp.getProperty(0).toString());
						name=temp.getProperty(1).toString();
						user_name=temp.getProperty(2).toString();
						email=temp.getProperty(3).toString();
						user_type_id=Integer.parseInt(temp.getProperty(4).toString());
						register_date=temp.getProperty(5).toString();
						average_rating=Float.parseFloat(temp.getProperty(6).toString());
						// Get the first property and change the label text
						Toast.makeText(getApplicationContext(),
								result.getProperty(0).toString(),
								Toast.LENGTH_LONG).show();
					} else {
						Toast.makeText(getApplicationContext(), "No Response",
								Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		final ListView listview = (ListView) findViewById(R.id.listViewTop);
		String[] values = new String[] { "Functional Programming with Scala","Introduction to computing",
				"Irrational behavior","Game Theory","Non-linear dynamics","Elementary differantial equations" };

		final ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < values.length; ++i) {
			list.add(values[i]);
		}
		final StableArrayAdapter adapter = new StableArrayAdapter(this,
				android.R.layout.simple_list_item_1, list);
		listview.setAdapter(adapter);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	
}
